var isOn;
var respRSS;
var tabID;
const browserAction = chrome.browserAction;

function reloadDisplay() {
    var icon, tooltip;
    isOn = getHightlightTM();
    if (isOn) {
        icon = 'on.png';
        tooltip = 'Get updates on the latest gigs and events from Ticketmaster';
    } else {
        icon = 'off.png';
        tooltip = 'Get updates on the latest gigs and events from Ticketmaster';
    }

    browserAction.setIcon({
        path: icon
    });
    browserAction.setTitle({
        title: tooltip
    });
}

function resetRSS() {
	respRSS = undefined;
}

function broadcastState() {
    chrome.windows.getAll({
        populate: true
    },

    function (windows) {
        for (var i = 0; i < windows.length; i++) {
            var viewport = windows[i];
            chrome.tabs.getSelected(viewport.id, function (tab) {
                chrome.tabs.connect(tab.id).postMessage({
                    isOn: isOn,
                    respRSS: respRSS,
                    tabID: tab.id
                });
            })
        }
    });
}


function setExtensionText(strText) {
    browserAction.setBadgeText({
        text: strText
    })
}

chrome.extension.onConnect.addListener(function (port) {
    port.onMessage.addListener(function (data) {
        isOn = getHightlightTM();
        reloadDisplay();
        broadcastState();
    });
});

chrome.extension.onRequest.addListener(

function (request, sender, sendResponse) {
    if (request.getData == "rss") {
        $.getJSON(getJSONTM(), function (data) {
            sendResponse({
                rssData: data
            });
        });
    } else sendResponse({}); // snub them.
});